﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace UI.TileEngine
{
    public class TileLayer
    {

        private Tile[,] _tiles;
        private int _width;
        private int _height;

        public TileLayer(int width, int height)
        {
            this._tiles = new Tile[width, height];
            this._width = width;
            this._height = height;
        }

        public void setTile(Point position, Tile tile)
        {
            this._tiles[position.X, position.Y] = tile;
        }

        public Tile getTile(Point position)
        {
            return this._tiles[position.X, position.Y];
        }

        public bool hasTile(Point position)
        {
            if (position.X < 0 || position.Y < 0 || position.X >= this._width || position.Y >= this._height) { return false; }
            return this._tiles[position.X, position.Y] != null;
        }

        public void draw(Rectangle position, SpriteBatch batch)
        {
            for (int y = position.Y; y <= position.Height + position.Y; y++)
            {
                for (int x = position.X; x <= position.Width + position.X; x++)
                {
                    //Get the tile to draw itself, it will use the layer to find neighboors and the tileset to get the right item to draw
                    if (this.hasTile(new Point(x, y)))
                    {
                        this.getTile(new Point(x, y)).draw(batch);
                    }
                }
            }
        }

    }
}
